package com.xp.basic.heima.nio.netty.simpledemo.simple;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

import java.net.InetSocketAddress;

public class TestNettyServerDemo {
    public static void main(String[] args) {
        new ServerBootstrap()
                //这是事件处理组，可以理解为多线程 + selector
                .group(new NioEventLoopGroup())
                //这是选择实现的类
                .channel(NioServerSocketChannel.class)
                //这里规定一系列的处理类
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel sc) throws Exception {
                        //这里是channel 通道对应的管道
                        sc.pipeline().addLast(new StringDecoder());
                        sc.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
                                System.out.println(" msg : "+ s);
                            }
                        });
                    }
                })
                //指定服务器绑定的 ip:端口
                .bind(new InetSocketAddress(8080));
    }
}
